/*
 * Copyright (c) 2009, Manish Shakya,Real Time Solutions Pvt. Ltd.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *
 */
/**
*
*	\file  irq.c
*	\author Manish Shakya 
*	\brief
*	\date  Tuesday,October 27,2009
*	\version 1.0
*
*
*/

#include "peripherals.h"
#ifdef __IRQ__
/***********************************************************************************************************************/
void 
irq_init(void) 
{
    uint32_t  i = 0;
    uint32_t *vect_addr;
    uint32_t *vect_ctrl;

	/* initialize VIC*/
    VICIntEnClr = 0xffffffff;
    VICVectAddr = 0;
    VICIntSelect = 0;

    /* set all the vector and vector control register to 0 */
    for ( i = 0; i < VIC_SIZE; i++ ){
        vect_addr = (uint32_t *)(VIC_BASE_VECT_ADDR + i*4);
        vect_ctrl = (uint32_t *)(VIC_BASE_CTRL_ADDR  + i*4);
        *vect_addr = 0x0;
        *vect_ctrl=0x0;
    }
}

/***********************************************************************************************************************/
/*!
	\Name 
	\param 
	\return
	\sa	
	\brief	
*/

void 
irq_install(uint32_t int_number, void (*handler_addr)(void) __irq )
{
    
    uint32_t *vect_addr;
    uint32_t *vect_ctrl;
    uint32_t i;

    ASSERT(int_number<VIC_SIZE);
    ASSERT(handler_addr);

	
    VICIntEnClr = 1 << int_number;  /* Disable Interrupt */
    for (i=0;i<VIC_SIZE;i++){

        vect_addr = (uint32_t *)(VIC_BASE_VECT_ADDR + i*4);
        vect_ctrl = (uint32_t *)(VIC_BASE_CTRL_ADDR + i*4);
        if (*vect_addr==0){

            *vect_addr = (uint32_t)handler_addr;    
            *vect_ctrl =  (1<<5)|(int_number);
            break;
        }
    }
    VICIntEnable = 1 << int_number; /* Enable Interrupt */
}

#endif

